package cn.tedu.nio.buffer;
import java.nio.ByteBuffer;

public class ByteBufferDemo2 {
    public static void main(String[] args) {
        // 数据已知的情况下，可以这么创建buffer
        ByteBuffer buffer = ByteBuffer.wrap("Hello big1910".getBytes());
        // 缓冲区刚创建，position指向第0位
        System.out.println(buffer.position());

        // 遍历缓冲区, 获取缓冲区对应的字节数组
        //byte[] data = buffer.array();
        //System.out.println(new String(data));

        ByteBuffer buffer1 = ByteBuffer.allocate(100);
        buffer1.put("Hello big1910".getBytes());
        // 有flip，字符串长度是buffer1.limit()
        // 无flip，字符串长度是buffer1.position()
        buffer1.flip();
        byte[] data1 = buffer1.array();
//        System.out.println(new String(data1, 0, buffer1.position()));
        System.out.println(new String(data1, 0, buffer1.limit()));
    }
}
